/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package trabalho;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jayme
 */
public class SplitQueries {
    
    public ArrayList<MyQuery> getCran(String benchMarkQueries){
        ArrayList<MyQuery> queries = new ArrayList<>();
        
        try {  
            System.out.println("Processando consultas");
            Scanner scanner = new Scanner(new FileReader(benchMarkQueries)).useDelimiter(".I");
            int id;
            String text, aux; 
            while (scanner.hasNext()){
                String queryFull = scanner.next();
                
                String[] queryArray = queryFull.split(".W");
                
                aux = queryArray[0].replace(" ", "");
                aux = aux.replace("\n", "");
                aux = aux.replace("\r", "");
                aux = aux.replace("\t", ""); 
                id = Integer.parseInt(aux);  
             
                text = (queryArray[1].replace("\n", ""));
                //System.out.println("lendo consulta: "+id);
                queries.add(new MyQuery(id, text));
            }
            System.out.println("Feito");
        
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Trabalho.class.getName()).log(Level.SEVERE, null, ex);
        }
        return queries;
    }
    
    public ArrayList<MyQuery> getCacm(String benchMarkQueries){
        ArrayList<MyQuery> queries = new ArrayList<>();
        
        try {  
            //ta com erro, está delimitando em todos I
            Scanner scanner = new Scanner(new FileReader(benchMarkQueries)).useDelimiter("-->I");
            int id;
            String text; 
            String aux;
            while (scanner.hasNext()){
                String queryFull = scanner.next();
                
                //Esta dando split em todo W
                String[] queryArray = queryFull.split("-->W");
                aux = (((queryArray[0].replace(" ", "")).replace("\n","")).replace("\r","")).replace("\t","");
     
                id = Integer.parseInt(aux);
                       
                queryArray = queryArray[1].split("-->N");
                text = (((queryArray[0].replace("\n", "")).replace("\r", "")).replace("\t", "")).replaceAll( "\u001a", "");
                //System.out.println(id);
                //System.out.println(text);

                queries.add(new MyQuery(id, text));
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Trabalho.class.getName()).log(Level.SEVERE, null, ex);
        }
        return queries;
    }
}
